## creates figure for rich-middle income results
## figure 4

rm(list=ls())

library(tidyverse)


# read in bootstrapped opinion data

opinion_bootstrapped <- readRDS('bootstrap_gaps_data/opinion_bootstrapped_noleaners.RDS')

for (i in names(opinion_bootstrapped)) {
  assign(i, opinion_bootstrapped[[i]])
}


# set up matrices/vectors of polarization data
# to feed into plots/figures

dem_polarization_boot_50 <- abs(dem_top_boot - dem_middle_boot)
rep_polarization_boot_50 <- abs(rep_top_boot - rep_middle_boot)
all_polarization_boot_50 <- abs(all_top_boot - all_middle_boot)

dem_issue_polarization_50 <- apply(dem_polarization_boot_50, MARGIN = 1, median)
rep_issue_polarization_50 <- apply(rep_polarization_boot_50, MARGIN = 1, median)
all_issue_polarization_50 <- apply(all_polarization_boot_50, MARGIN = 1, median)

dem_avg_polarization_50 <- apply(dem_polarization_boot_50, MARGIN = 2, mean)
rep_avg_polarization_50 <- apply(rep_polarization_boot_50, MARGIN = 2, mean)
all_avg_polarization_50 <- apply(all_polarization_boot_50, MARGIN = 2, mean)

dem_med_polarization_50 <- apply(dem_polarization_boot_50, MARGIN = 2, median)
rep_med_polarization_50 <- apply(rep_polarization_boot_50, MARGIN = 2, median)
all_med_polarization_50 <- apply(all_polarization_boot_50, MARGIN = 2, median)

diff_polarization_boot_50 <- rep_polarization_boot_50 - dem_polarization_boot_50


# figure 4 - middle income thresholds

thresholds_df_50 <- expand.grid(question = issuetopics$question,
                                party = c('rep', 'dem', 'all'),
                                threshold = (0:50)/100,
                                polarized = NA,
                                stringsAsFactors = FALSE)

for (i in 1:nrow(thresholds_df_50)) {
  if (i %% floor(nrow(thresholds_df_50)/10) == 0) cat('|')
  temp.rich <- get(paste0(thresholds_df_50$party[i], '_top_boot'))
  temp.rich <- temp.rich[thresholds_df_50$question[i],]
  temp.mid <- get(paste0(thresholds_df_50$party[i], '_middle_boot'))
  temp.mid <- temp.mid[thresholds_df_50$question[i],]
  
  t <- thresholds_df_50$threshold[i]
  
  if (median(abs(temp.rich - temp.mid)) >= t) { 
    thresholds_df_50$polarized[i] <- 1
  } else {
    thresholds_df_50$polarized[i] <- 0
  }
}

thresholdsplot_50.df <- thresholds_df_50 %>% 
  mutate(party = case_when(party == 'dem' ~ 'Democrats',
                           party == 'rep' ~ 'Republicans',
                           party == 'all' ~ 'All Respondents')) %>% 
  group_by(party, threshold) %>% 
  summarize(n_pol = sum(polarized)) %>% 
  ungroup() %>% 
  filter(threshold <= 0.25)

ggplot(data = thresholdsplot_50.df,
       aes(x = threshold, y = n_pol / nrow(issuetopics))) + 
  geom_point(aes(color = party, shape = party), size = 2, alpha = 0.4) + 
  geom_line(aes(color = party), size = 0.75, alpha = 0.5) + 
  labs(title = '',
       y = 'Share of Questions with Opinion Gap',
       x = 'Threshold',
       color = '', shape = '') +
  scale_color_manual(values = c('gray40', 'blue', 'red')) +
  theme_bw() +
  theme(legend.position = 'bottom')
ggsave('figures/middle_num_polarized_by_threshold.png', height = 4, width = 5.5)
